<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 6.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/works/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/works/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/works/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/works/images/logo.svg" color="#222">

<link rel="stylesheet" href="/works/css/main.css">

<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Consolas:300,300italic,400,400italic,700,700italic|PingFang SC:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">
<link rel="stylesheet" href="/works/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.css">
  <link rel="stylesheet" href="/works/lib/pace/pace-theme-bounce.min.css">
  <script src="/works/lib/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"chudod.gitee.io","root":"/works/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"hide","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":"mac"},"back2top":{"enable":true,"sidebar":true,"scrollpercent":true},"bookmark":{"enable":true,"color":"#222","save":"auto"},"fancybox":true,"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"manual","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":true,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="该页面整理了MPI_Allreduce在OpenMPI和MPICH中的设计与最佳算法选择。">
<meta property="og:type" content="article">
<meta property="og:title" content="MPI_Allreduce的在OpenMPI、MPICH中的实现">
<meta property="og:url" content="https://chudod.gitee.io/works/2022/04/13/OpenMPI&MPICH_Allreduce/index.html">
<meta property="og:site_name" content="Computing Dynamics">
<meta property="og:description" content="该页面整理了MPI_Allreduce在OpenMPI和MPICH中的设计与最佳算法选择。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://note.youdao.com/yws/api/personal/file/WEBef75c5797a8fe9a7d3afe7d9f84db3a2?method=download&shareKey=0412dcb1d5f95516723864a4f1a48a13">
<meta property="og:image" content="https://note.youdao.com/yws/api/personal/file/WEBe38b3f100dd6599ec413faa1ee25edcf?method=download&shareKey=0412dcb1d5f95516723864a4f1a48a13">
<meta property="article:published_time" content="2022-04-12T16:00:00.000Z">
<meta property="article:modified_time" content="2022-05-01T09:13:20.991Z">
<meta property="article:author" content="虎王">
<meta property="article:tag" content="MPI">
<meta property="article:tag" content="并行计算">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://note.youdao.com/yws/api/personal/file/WEBef75c5797a8fe9a7d3afe7d9f84db3a2?method=download&shareKey=0412dcb1d5f95516723864a4f1a48a13">

<link rel="canonical" href="https://chudod.gitee.io/works/2022/04/13/OpenMPI&MPICH_Allreduce/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>MPI_Allreduce的在OpenMPI、MPICH中的实现 | Computing Dynamics</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/works/rss2.xml" title="Computing Dynamics" type="application/rss+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/works/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">Computing Dynamics</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">小王同学的工作站</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/works/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/works/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/works/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/works/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/works/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
        <li class="menu-item menu-item-schedule">

    <a href="/works/schedule/" rel="section"><i class="fa fa-calendar fa-fw"></i>日程表</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container"></div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="algolia-results">
  <div id="algolia-stats"></div>
  <div id="algolia-hits"></div>
  <div id="algolia-pagination" class="algolia-pagination"></div>
</div>

      
    </div>
  </div>

</div>
    </header>

    
  <div class="reading-progress-bar"></div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>

  <a href="https://github.com/Chu-DoD" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://chudod.gitee.io/works/2022/04/13/OpenMPI&MPICH_Allreduce/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/works/images/avatar.png">
      <meta itemprop="name" content="虎王">
      <meta itemprop="description" content="喜欢造飞机导弹和计算机的大学生">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Computing Dynamics">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          MPI_Allreduce的在OpenMPI、MPICH中的实现
        </h1>

        <div class="post-meta">

        
          <i class="fa fa-thumb-tack"></i>
          <font color=7D26CD>置顶</font>
          <span class="post-meta-divider">|</span>
        
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2022-04-13 00:00:00" itemprop="dateCreated datePublished" datetime="2022-04-13T00:00:00+08:00">2022-04-13</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2022-05-01 17:13:20" itemprop="dateModified" datetime="2022-05-01T17:13:20+08:00">2022-05-01</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/works/categories/%E5%B9%B6%E8%A1%8C%E8%AE%A1%E7%AE%97/" itemprop="url" rel="index"><span itemprop="name">并行计算</span></a>
                </span>
                  ，
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/works/categories/%E5%B9%B6%E8%A1%8C%E8%AE%A1%E7%AE%97/MPI%E9%9B%86%E5%90%88%E9%80%9A%E4%BF%A1%E7%AE%97%E6%B3%95/" itemprop="url" rel="index"><span itemprop="name">MPI集合通信算法</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span>
            <div class="post-description">该页面整理了MPI_Allreduce在OpenMPI和MPICH中的设计与最佳算法选择。</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="Allreduce算法列表"><a href="#Allreduce算法列表" class="headerlink" title="Allreduce算法列表"></a>Allreduce算法列表</h1><h2 id="1、OpenMPI-4-1中的MPI-Allreduce算法"><a href="#1、OpenMPI-4-1中的MPI-Allreduce算法" class="headerlink" title="1、OpenMPI-4.1中的MPI_Allreduce算法"></a>1、OpenMPI-4.1中的MPI_Allreduce算法</h2><p>$\verb+OpenMPI-4.1.2+$是最新版本的$\verb+OpenMPI+$，算法的具体选择在$\verb+ompi&#x2F;mca&#x2F;coll&#x2F;tuned&#x2F;coll_tuned_decision_fixed.c+$和$\verb+ompi&#x2F;mca&#x2F;coll&#x2F;tuned&#x2F;coll_tuned_decision_dynamic.c+$文件里，用户可以指定规则以及选择使用的算法，并且$\verb+OpenMPI+$使用了6种算法，分别是</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">Algorithms:</span><br><span class="line">   &#123;<span class="number">1</span>, <span class="string">&quot;basic_linear&quot;</span>&#125;: Reduce + Broadcast</span><br><span class="line">   &#123;<span class="number">2</span>, <span class="string">&quot;nonoverlapping&quot;</span>&#125;: Reduce +Broadcast</span><br><span class="line">   &#123;<span class="number">3</span>, <span class="string">&quot;recursive_doubling&quot;</span>&#125;: Recursive Doubling</span><br><span class="line">   &#123;<span class="number">4</span>, <span class="string">&quot;ring&quot;</span>&#125;: <span class="built_in">Ring</span>(Segmented Messages) + <span class="built_in">Allgather</span>(Ring)</span><br><span class="line">   &#123;<span class="number">5</span>, <span class="string">&quot;segmented_ring&quot;</span>&#125;: Segmented Ring</span><br><span class="line">   &#123;<span class="number">6</span>, <span class="string">&quot;rabenseifner&quot;</span>&#125;: Reduce-Scatter + Allgather</span><br><span class="line">   <span class="comment">/* Currently, ring, segmented ring, and rabenseifner do not support non-commutative operations. */</span></span><br></pre></td></tr></table></figure>
<p>默认使用$\verb+&#x2F;coll_tuned_decision_fixed.c+$里的规则，具体的选择方法如下(原代码是100多行的$else-if$，贼暴力)：<br><img data-src="https://note.youdao.com/yws/api/personal/file/WEBef75c5797a8fe9a7d3afe7d9f84db3a2?method=download&shareKey=0412dcb1d5f95516723864a4f1a48a13"><br><img data-src="https://note.youdao.com/yws/api/personal/file/WEBe38b3f100dd6599ec413faa1ee25edcf?method=download&shareKey=0412dcb1d5f95516723864a4f1a48a13"><br>除了默认的规则之外，用户还可以指定参数来选择对应的算法。  </p>
<h2 id="2、MPICH中的MPI-Allreduce算法"><a href="#2、MPICH中的MPI-Allreduce算法" class="headerlink" title="2、MPICH中的MPI_Allreduce算法"></a>2、MPICH中的MPI_Allreduce算法</h2><p>$\verb+MPICH-4.0.2+$是最新版本的$\verb+MPICH+$，在$\verb+MPICH+$中调用$\verb+MPIR_Csel_search+$函数来确定参数，$\verb+MPIR_Csel_search+$返回的值决定采用何种算法。算法的选择逻辑在$\verb+mpich-4.0.2&#x2F;src&#x2F;mpi&#x2F;coll&#x2F;mpir_coll.c+$的4496行的$\verb+MPIR_Allreduce_allcomm_auto+$函数处，$\verb+MPICH+$中的5种算法如下：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Algorithms:</span><br><span class="line">    <span class="number">1</span>-&gt; <span class="string">&quot;MPIR_Allreduce_intra_smp&quot;</span>: Reduce + Broadcast;</span><br><span class="line">    <span class="number">2</span>-&gt; <span class="string">&quot;MPIR_Allreduce_intra_recursive_doubling&quot;</span>: Recursive Doubling;</span><br><span class="line">    <span class="number">3</span>-&gt; <span class="string">&quot;MPIR_Allreduce_intra_reduce_scatter_allgather&quot;</span>: Scatter + Allgather;</span><br><span class="line">    <span class="number">4</span>-&gt; <span class="string">&quot;MPIR_Allreduce_inter_reduce_exchange_bcast&quot;</span>: a variant of Reduce + Broadcast;</span><br><span class="line">    <span class="number">5</span>-&gt; <span class="string">&quot;MPIR_Allreduce_allcomm_nb&quot;</span>: Nonblocking algorithm, namely MPIR_Iallreduce;</span><br></pre></td></tr></table></figure>

<h1 id="Allreduce算法实现"><a href="#Allreduce算法实现" class="headerlink" title="Allreduce算法实现"></a>Allreduce算法实现</h1><h2 id="1、-Reduce-Broadcast-算法"><a href="#1、-Reduce-Broadcast-算法" class="headerlink" title="1、$Reduce$+$Broadcast$算法"></a>1、$Reduce$+$Broadcast$算法</h2><h3 id="OpenMPI版"><a href="#OpenMPI版" class="headerlink" title="OpenMPI版"></a>OpenMPI版</h3><h4 id="basic-linear函数"><a href="#basic-linear函数" class="headerlink" title="basic_linear函数"></a>basic_linear函数</h4><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="type">int</span> <span class="title">ompi_coll_base_allreduce_intra_basic_linear</span><span class="params">(<span class="type">const</span> <span class="type">void</span> *sbuf, <span class="type">void</span> *rbuf, <span class="type">int</span> count, </span></span></span><br><span class="line"><span class="params"><span class="function">                                                <span class="keyword">struct</span> <span class="type">ompi_datatype_t</span> *dtype, </span></span></span><br><span class="line"><span class="params"><span class="function">                                                <span class="keyword">struct</span> <span class="type">ompi_op_t</span> *op, </span></span></span><br><span class="line"><span class="params"><span class="function">                                                <span class="keyword">struct</span> <span class="type">ompi_communicator_t</span> *comm,</span></span></span><br><span class="line"><span class="params"><span class="function">                                                <span class="type">mca_coll_base_module_t</span> *<span class="keyword">module</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">/* Reduce to 0 and broadcast. */</span></span><br><span class="line">    <span class="keyword">if</span> (MPI_IN_PLACE == sbuf) &#123;</span><br><span class="line">        <span class="keyword">if</span>(<span class="number">0</span> == rank)  </span><br><span class="line">            err = <span class="built_in">ompi_coll_base_reduce_intra_basic_linear</span> (MPI_IN_PLACE, rbuf, count, dtype, op, <span class="number">0</span>, comm, <span class="keyword">module</span>);</span><br><span class="line">        <span class="keyword">else</span> </span><br><span class="line">            err = <span class="built_in">ompi_coll_base_reduce_intra_basic_linear</span>(rbuf, <span class="literal">NULL</span>, ...);</span><br><span class="line">    &#125; </span><br><span class="line">    <span class="keyword">else</span> &#123;</span><br><span class="line">        err = <span class="built_in">ompi_coll_base_reduce_intra_basic_linear</span>(sbuf, rbuf, ...);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (MPI_SUCCESS != err) <span class="keyword">return</span> err;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">ompi_coll_base_bcast_intra_basic_linear</span>(rbuf, count, dtype, <span class="number">0</span>, comm, <span class="keyword">module</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>其中$\verb+MPI_IN_PLACE+$参数用在$\verb+MPI_GATHER+$、$\verb+MPI_Reduce+$等有$\verb+send_buf+$和$\verb+recv_buf+$的函数中，用来代替$\verb+send_buf+$，说明当前进程既发送又接受数据，而且要发送的数据和在要接收的数据的保存在同一内存。</p>
<h4 id="nonoverlapping函数"><a href="#nonoverlapping函数" class="headerlink" title="nonoverlapping函数"></a>nonoverlapping函数</h4><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="type">int</span> <span class="title">ompi_coll_base_allreduce_intra_nonoverlapping</span><span class="params">(<span class="type">const</span> <span class="type">void</span> *sbuf, <span class="type">void</span> *rbuf, <span class="type">int</span> count, </span></span></span><br><span class="line"><span class="params"><span class="function">                                                <span class="keyword">struct</span> <span class="type">ompi_datatype_t</span> *dtype, </span></span></span><br><span class="line"><span class="params"><span class="function">                                                <span class="keyword">struct</span> <span class="type">ompi_op_t</span> *op, </span></span></span><br><span class="line"><span class="params"><span class="function">                                                <span class="keyword">struct</span> <span class="type">ompi_communicator_t</span> *comm,</span></span></span><br><span class="line"><span class="params"><span class="function">                                                <span class="type">mca_coll_base_module_t</span> *<span class="keyword">module</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    rank = <span class="built_in">ompi_comm_rank</span>(comm);</span><br><span class="line"></span><br><span class="line">    <span class="comment">//第一个参数是sbuf 或者是 MPI_IN_PLACE</span></span><br><span class="line">    comm-&gt;c_coll-&gt;<span class="built_in">coll_reduce</span> (sbuf, rbuf, count, dtype, op, <span class="number">0</span>, comm, comm-&gt;c_coll-&gt;coll_reduce_module);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (MPI_SUCCESS != err) <span class="keyword">return</span> err;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> comm-&gt;c_coll-&gt;<span class="built_in">coll_bcast</span>(rbuf, count, dtype, <span class="number">0</span>, comm, comm-&gt;c_coll-&gt;coll_bcast_module);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="MPICH版"><a href="#MPICH版" class="headerlink" title="MPICH版"></a>MPICH版</h3><h4 id=""><a href="#" class="headerlink" title=""></a></h4><h2 id="2、-Recursive-Doubling-算法"><a href="#2、-Recursive-Doubling-算法" class="headerlink" title="2、$Recursive$ $Doubling$算法"></a>2、$Recursive$ $Doubling$算法</h2><h3 id="OpenMPI版-1"><a href="#OpenMPI版-1" class="headerlink" title="OpenMPI版"></a>OpenMPI版</h3><h4 id="recursivedoubling函数"><a href="#recursivedoubling函数" class="headerlink" title="recursivedoubling函数"></a>recursivedoubling函数</h4><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*         算法保持了规约操作的顺序，所以可支持满足以及不满足交换律的操作</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> *         Example on 7 nodes:</span></span><br><span class="line"><span class="comment"> *         Initial state</span></span><br><span class="line"><span class="comment"> *         #      0       1      2       3      4       5      6</span></span><br><span class="line"><span class="comment"> *               [0]     [1]    [2]     [3]    [4]     [5]    [6]</span></span><br><span class="line"><span class="comment"> *         Initial adjustment step for non-power of two nodes.</span></span><br><span class="line"><span class="comment"> *         old rank      1              3              5      6</span></span><br><span class="line"><span class="comment"> *         new rank      0              1              2      3</span></span><br><span class="line"><span class="comment"> *                     [0+1]          [2+3]          [4+5]   [6]</span></span><br><span class="line"><span class="comment"> *         Step 1</span></span><br><span class="line"><span class="comment"> *         old rank      1              3              5      6</span></span><br><span class="line"><span class="comment"> *         new rank      0              1              2      3</span></span><br><span class="line"><span class="comment"> *                     [0+1+]         [0+1+]         [4+5+]  [4+5+]</span></span><br><span class="line"><span class="comment"> *                     [2+3+]         [2+3+]         [6   ]  [6   ]</span></span><br><span class="line"><span class="comment"> *         Step 2</span></span><br><span class="line"><span class="comment"> *         old rank      1              3              5      6</span></span><br><span class="line"><span class="comment"> *         new rank      0              1              2      3</span></span><br><span class="line"><span class="comment"> *                     [0+1+]         [0+1+]         [0+1+]  [0+1+]</span></span><br><span class="line"><span class="comment"> *                     [2+3+]         [2+3+]         [2+3+]  [2+3+]</span></span><br><span class="line"><span class="comment"> *                     [4+5+]         [4+5+]         [4+5+]  [4+5+]</span></span><br><span class="line"><span class="comment"> *                     [6   ]         [6   ]         [6   ]  [6   ]</span></span><br><span class="line"><span class="comment"> *         Final adjustment step for non-power of two nodes</span></span><br><span class="line"><span class="comment"> *         #      0       1      2       3      4       5      6</span></span><br><span class="line"><span class="comment"> *              [0+1+] [0+1+] [0+1+]  [0+1+] [0+1+]  [0+1+] [0+1+]</span></span><br><span class="line"><span class="comment"> *              [2+3+] [2+3+] [2+3+]  [2+3+] [2+3+]  [2+3+] [2+3+]</span></span><br><span class="line"><span class="comment"> *              [4+5+] [4+5+] [4+5+]  [4+5+] [4+5+]  [4+5+] [4+5+]</span></span><br><span class="line"><span class="comment"> *              [6   ] [6   ] [6   ]  [6   ] [6   ]  [6   ] [6   ]</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">ompi_coll_base_allreduce_intra_recursivedoubling</span><span class="params">(<span class="type">const</span> <span class="type">void</span> *sbuf, <span class="type">void</span> *rbuf, <span class="type">int</span> count, </span></span></span><br><span class="line"><span class="params"><span class="function">                                                    <span class="keyword">struct</span> <span class="type">ompi_datatype_t</span> *dtype, </span></span></span><br><span class="line"><span class="params"><span class="function">                                                    <span class="keyword">struct</span> <span class="type">ompi_op_t</span> *op, </span></span></span><br><span class="line"><span class="params"><span class="function">                                                    <span class="keyword">struct</span> <span class="type">ompi_communicator_t</span> *comm,</span></span></span><br><span class="line"><span class="params"><span class="function">                                                    <span class="type">mca_coll_base_module_t</span> *<span class="keyword">module</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="type">int</span> size = <span class="built_in">ompi_comm_size</span>(comm);</span><br><span class="line">    <span class="type">int</span> rank = <span class="built_in">ompi_comm_rank</span>(comm);</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">if</span> (<span class="number">1</span> == size) &#123;</span><br><span class="line">        <span class="keyword">if</span> (MPI_IN_PLACE != sbuf) <span class="comment">//copy data form sbuf to rbuf;</span></span><br><span class="line">        <span class="keyword">return</span> MPI_SUCCESS;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/* Determine nearest power of two less than or equal to size */</span></span><br><span class="line">    adjsize = <span class="built_in">opal_next_poweroftwo</span> (size);</span><br><span class="line">    adjsize &gt;&gt;= <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/* Handle non-power-of-two case:</span></span><br><span class="line"><span class="comment">       - Even ranks less than 2 * extra_ranks send their data to (rank + 1), and sets new rank to -1.</span></span><br><span class="line"><span class="comment">       - Odd ranks less than 2 * extra_ranks receive data from (rank - 1), apply appropriate operation, and set new rank to rank/2</span></span><br><span class="line"><span class="comment">       - Everyone else sets rank to rank - extra_ranks</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line"></span><br><span class="line">    extra_ranks = size - adjsize;</span><br><span class="line">    <span class="keyword">if</span> (rank &lt;  (<span class="number">2</span> * extra_ranks)) </span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="number">0</span> == (rank % <span class="number">2</span>)) &#123;</span><br><span class="line">            <span class="built_in">MCA_PML_CALL</span>(<span class="built_in">send</span>(tmpsend, count, dtype, (rank + <span class="number">1</span>), ..., comm));</span><br><span class="line">            newrank = <span class="number">-1</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="built_in">MCA_PML_CALL</span>(<span class="built_in">recv</span>(tmprecv, count, dtype, (rank - <span class="number">1</span>), ..., comm, ...));</span><br><span class="line">            <span class="comment">/* tmpsend = tmprecv (op) tmpsend */</span></span><br><span class="line">            <span class="built_in">ompi_op_reduce</span>(op, tmprecv, tmpsend, count, dtype);</span><br><span class="line">            newrank = rank &gt;&gt; <span class="number">1</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span> &#123;newrank = rank - extra_ranks;&#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/* Communication/Computation loop</span></span><br><span class="line"><span class="comment">       - Exchange message with remote node.</span></span><br><span class="line"><span class="comment">       - Perform appropriate operation taking in account order of operations:</span></span><br><span class="line"><span class="comment">       result = value (op) result</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">   <span class="keyword">for</span> (distance = <span class="number">0x1</span>; distance &lt; adjsize; distance &lt;&lt;=<span class="number">1</span>) &#123;</span><br><span class="line">        <span class="keyword">if</span> (newrank &lt; <span class="number">0</span>) <span class="keyword">break</span>;</span><br><span class="line">        <span class="comment">/* Determine remote node (异或操作)*/</span></span><br><span class="line">        newremote = newrank ^ distance;</span><br><span class="line">        remote = (newremote &lt; extra_ranks)? (newremote * <span class="number">2</span> + <span class="number">1</span>):(newremote + extra_ranks);</span><br><span class="line"></span><br><span class="line">        <span class="comment">/* Exchange the data */</span></span><br><span class="line">        ret = <span class="built_in">ompi_coll_base_sendrecv_actual</span>(tmpsend, count, dtype, remote, ..., tmprecv, count, dtype, remote, ..., comm, ...);</span><br><span class="line"></span><br><span class="line">        <span class="comment">/* Apply operation */</span></span><br><span class="line">        <span class="keyword">if</span> (rank &lt; remote) &#123;</span><br><span class="line">            <span class="comment">/* tmprecv = tmpsend (op) tmprecv, ompi_op_reduce(op, soruce, target, ...)*/</span></span><br><span class="line">            <span class="built_in">ompi_op_reduce</span>(op, tmpsend, tmprecv, count, dtype);</span><br><span class="line">            tmpswap = tmprecv;</span><br><span class="line">            tmprecv = tmpsend;</span><br><span class="line">            tmpsend = tmpswap;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="comment">/* tmpsend = tmprecv (op) tmpsend */</span></span><br><span class="line">            <span class="built_in">ompi_op_reduce</span>(op, tmprecv, tmpsend, count, dtype);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/* Handle non-power-of-two case:</span></span><br><span class="line"><span class="comment">       - Odd ranks less than 2 * extra_ranks send result from tmpsend to (rank - 1)</span></span><br><span class="line"><span class="comment">       - Even ranks less than 2 * extra_ranks receive result from (rank + 1)</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">    <span class="keyword">if</span> (rank &lt; (<span class="number">2</span> * extra_ranks)) &#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="number">0</span> == (rank % <span class="number">2</span>)) &#123;</span><br><span class="line">            <span class="built_in">MCA_PML_CALL</span>(<span class="built_in">recv</span>(rbuf, count, dtype, (rank + <span class="number">1</span>), ...));</span><br><span class="line">            tmpsend = (<span class="type">char</span>*)rbuf;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="built_in">MCA_PML_CALL</span>(<span class="built_in">send</span>(tmpsend, count, dtype, (rank - <span class="number">1</span>), ...));</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/* Ensure that the final result is in rbuf */</span></span><br><span class="line">    <span class="keyword">if</span> (tmpsend != rbuf) <span class="comment">//copy form tmpsend to rbuf;</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> MPI_SUCCESS;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>注意新版$OpenMPI$中的$\verb+recursive doubling+$函数可以支持不满足交换律的操作，但是必须满足结合律。</p>
<h3 id="MPICH版-1"><a href="#MPICH版-1" class="headerlink" title="MPICH版"></a>MPICH版</h3><p>实现几乎完全一样，故略。</p>
<h2 id="4、reduce-scatter算法"><a href="#4、reduce-scatter算法" class="headerlink" title="4、reduce-scatter算法"></a>4、reduce-scatter算法</h2><h3 id="OpenMPI版-2"><a href="#OpenMPI版-2" class="headerlink" title="OpenMPI版"></a>OpenMPI版</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* </span></span><br><span class="line"><span class="comment"> * This algorithm is a combination of a reduce-scatter implemented with</span></span><br><span class="line"><span class="comment"> * recursive vector halving and recursive distance doubling, followed either</span></span><br><span class="line"><span class="comment"> * by an allgather implemented with recursive doubling.</span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> * Limitations:</span></span><br><span class="line"><span class="comment"> *   count &gt;= 2^&#123;\floor&#123;\log_2 p&#125;&#125;</span></span><br><span class="line"><span class="comment"> *   commutative operations only</span></span><br><span class="line"><span class="comment"> *   intra-communicators only</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">ompi_coll_base_allreduce_intra_redscat_allgather</span><span class="params">(<span class="type">const</span> <span class="type">void</span> *sbuf, </span></span></span><br><span class="line"><span class="params"><span class="function">    <span class="type">void</span> *rbuf, <span class="type">int</span> count, <span class="keyword">struct</span> <span class="type">ompi_datatype_t</span> *dtype,</span></span></span><br><span class="line"><span class="params"><span class="function">    <span class="keyword">struct</span> <span class="type">ompi_op_t</span> *op, <span class="keyword">struct</span> <span class="type">ompi_communicator_t</span> *comm,</span></span></span><br><span class="line"><span class="params"><span class="function">    <span class="type">mca_coll_base_module_t</span> *<span class="keyword">module</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="type">int</span> comm_size = <span class="built_in">ompi_comm_size</span>(comm);</span><br><span class="line">    <span class="type">int</span> rank = <span class="built_in">ompi_comm_rank</span>(comm);</span><br><span class="line"></span><br><span class="line">    <span class="type">int</span> nsteps = <span class="built_in">opal_hibit</span>(comm_size, comm-&gt;c_cube_dim + <span class="number">1</span>);<span class="comment">/*ilog2(comm_size)*/</span></span><br><span class="line">    <span class="built_in">assert</span>(nsteps &gt;= <span class="number">0</span>);</span><br><span class="line">    <span class="type">int</span> nprocs_pof2 = <span class="number">1</span> &lt;&lt; nsteps;                   <span class="comment">/* flp2(comm_size) */</span></span><br><span class="line"></span><br><span class="line">    <span class="type">ptrdiff_t</span> lb, extent, dsize, gap = <span class="number">0</span>;</span><br><span class="line">    <span class="built_in">ompi_datatype_get_extent</span>(dtype, &amp;lb, &amp;extent);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (count &lt; nprocs_pof2 || !<span class="built_in">ompi_op_is_commute</span>(op)) </span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">ompi_coll_base_allreduce_intra_basic_linear</span>(sbuf, rbuf, count, dtype, op, comm, <span class="keyword">module</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (sbuf != MPI_IN_PLACE) <span class="comment">//copy from sbuf to rbuf;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">     * Step 1. Reduce the number of processes to the nearest lower power of two</span></span><br><span class="line"><span class="comment">     * p&#x27; = 2^&#123;\floor&#123;\log_2 p&#125;&#125; by removing r = p - p&#x27; processes.</span></span><br><span class="line"><span class="comment">     * 1. All the even ranks send the second half of  the input vector to their right neighbor (rank + 1),</span></span><br><span class="line"><span class="comment">     *    and all the odd ranks send the first half of the input vector to their left neighbor (rank - 1).</span></span><br><span class="line"><span class="comment">     * 2. All 2r processes compute the reduction on their half.</span></span><br><span class="line"><span class="comment">     * 3. The odd ranks then send the result to their left neighbors (the even ranks).</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="type">int</span> vrank, step, wsize;</span><br><span class="line">    <span class="type">int</span> nprocs_rem = comm_size - nprocs_pof2;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (rank &lt; <span class="number">2</span> * nprocs_rem)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="type">int</span> count_lhalf = count / <span class="number">2</span>;</span><br><span class="line">        <span class="type">int</span> count_rhalf = count - count_lhalf;</span><br><span class="line">        <span class="keyword">if</span> (rank % <span class="number">2</span> != <span class="number">0</span>)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="comment">/*</span></span><br><span class="line"><span class="comment">             * Odd process -- exchange with (rank - 1)</span></span><br><span class="line"><span class="comment">             * Send the left half of the input vector to the left neighbor,</span></span><br><span class="line"><span class="comment">             * Recv the right half of the input vector from the left neighbor</span></span><br><span class="line"><span class="comment">             */</span></span><br><span class="line">            <span class="built_in">ompi_coll_base_sendrecv</span>(rbuf, count_lhalf, dtype, rank - <span class="number">1</span>, ...,</span><br><span class="line">                            (<span class="type">char</span> *)tmp_buf + (<span class="type">ptrdiff_t</span>)count_lhalf * extent,</span><br><span class="line">                            count_rhalf, dtype, rank - <span class="number">1</span>, ..., rank);</span><br><span class="line"></span><br><span class="line">            <span class="comment">/* Reduce on the right half of the buffers (result in rbuf) */</span></span><br><span class="line">            <span class="built_in">ompi_op_reduce</span>(op, (<span class="type">char</span> *)tmp_buf + (<span class="type">ptrdiff_t</span>)count_lhalf * extent,</span><br><span class="line">                        (<span class="type">char</span> *)rbuf + count_lhalf * extent, count_rhalf, dtype);</span><br><span class="line"></span><br><span class="line">            <span class="comment">/* Send the right half to the left neighbor */</span></span><br><span class="line">            <span class="built_in">MCA_PML_CALL</span>(<span class="built_in">send</span>((<span class="type">char</span> *)rbuf + (<span class="type">ptrdiff_t</span>)count_lhalf * extent,</span><br><span class="line">                               count_rhalf, dtype, rank - <span class="number">1</span>, ..., comm));</span><br><span class="line"></span><br><span class="line">            <span class="comment">/* This process does not pariticipate in recursive doubling phase */</span></span><br><span class="line">            vrank = <span class="number">-1</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">else</span></span><br><span class="line">        &#123;</span><br><span class="line">            <span class="comment">/*</span></span><br><span class="line"><span class="comment">             * Even process -- exchange with rank + 1</span></span><br><span class="line"><span class="comment">             * Send the right half of the input vector to the right neighbor,</span></span><br><span class="line"><span class="comment">             * Recv the left half of the input vector from the right neighbor</span></span><br><span class="line"><span class="comment">             */</span></span><br><span class="line">            <span class="built_in">ompi_coll_base_sendrecv</span>((<span class="type">char</span> *)rbuf + (<span class="type">ptrdiff_t</span>)count_lhalf*extent,</span><br><span class="line">                            count_rhalf, dtype, rank + <span class="number">1</span>, ...,tmp_buf, </span><br><span class="line">                            count_lhalf, dtype, rank + <span class="number">1</span>, ..., comm, ..., rank);</span><br><span class="line"></span><br><span class="line">            <span class="comment">/* Reduce on the left half of the buffers (result in rbuf) */</span></span><br><span class="line">            <span class="built_in">ompi_op_reduce</span>(op, tmp_buf, rbuf, count_lhalf, dtype);</span><br><span class="line"></span><br><span class="line">            <span class="comment">/* Recv the right half from the right neighbor */</span></span><br><span class="line">            <span class="built_in">MCA_PML_CALL</span>(<span class="built_in">recv</span>((<span class="type">char</span> *)rbuf + (<span class="type">ptrdiff_t</span>)count_lhalf * extent,</span><br><span class="line">                            count_rhalf, dtype, rank + <span class="number">1</span>, ..., comm, ...));</span><br><span class="line"></span><br><span class="line">            vrank = rank / <span class="number">2</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span> </span><br><span class="line">    &#123; <span class="comment">/* rank &gt;= 2 * nprocs_rem */</span></span><br><span class="line">        vrank = rank - nprocs_rem;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">     * Step 2. Reduce-scatter implemented with recursive vector halving and</span></span><br><span class="line"><span class="comment">     * recursive distance doubling. We have  new ranks and result in rbuf.</span></span><br><span class="line"><span class="comment">     *At the end, each process has 1 / p&#x27; of the total reduction result.</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="type">int</span>* rindex = <span class="built_in">malloc</span>(<span class="built_in">sizeof</span>(*rindex) * nsteps);</span><br><span class="line">    <span class="type">int</span>* sindex = <span class="built_in">malloc</span>(<span class="built_in">sizeof</span>(*sindex) * nsteps);</span><br><span class="line">    <span class="type">int</span>* rcount = <span class="built_in">malloc</span>(<span class="built_in">sizeof</span>(*rcount) * nsteps);</span><br><span class="line">    <span class="type">int</span>* scount = <span class="built_in">malloc</span>(<span class="built_in">sizeof</span>(*scount) * nsteps);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (vrank != <span class="number">-1</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        step = <span class="number">0</span>;</span><br><span class="line">        wsize = count;</span><br><span class="line">        sindex[<span class="number">0</span>] = rindex[<span class="number">0</span>] = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> mask = <span class="number">1</span>; mask &lt; nprocs_pof2; mask &lt;&lt;= <span class="number">1</span>)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="comment">/* On each iteration: rindex[step] = sindex[step] -- begining of the</span></span><br><span class="line"><span class="comment">            current window. Length of the current window is stored in wsize.*/</span></span><br><span class="line">            <span class="type">int</span> vdest = vrank ^ mask;</span><br><span class="line">            <span class="comment">/* Translate vdest virtual rank to real rank */</span></span><br><span class="line">            <span class="type">int</span> dest = (vdest &lt; nprocs_rem) ? vdest * <span class="number">2</span> : vdest + nprocs_rem;</span><br><span class="line">            <span class="keyword">if</span> (rank &lt; dest) &#123;</span><br><span class="line">                <span class="comment">/*</span></span><br><span class="line"><span class="comment">                 * Recv into the left half of the current window, send the right</span></span><br><span class="line"><span class="comment">                 * half of the window to the peer (perform reduce on the left</span></span><br><span class="line"><span class="comment">                 * half of the current window)</span></span><br><span class="line"><span class="comment">                 */</span></span><br><span class="line">                rcount[step] = wsize / <span class="number">2</span>;</span><br><span class="line">                scount[step] = wsize - rcount[step];</span><br><span class="line">                sindex[step] = rindex[step] + rcount[step];</span><br><span class="line">            &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="comment">/*</span></span><br><span class="line"><span class="comment">                 * Recv into the right half of the current window, send the left</span></span><br><span class="line"><span class="comment">                 * half of the window to the peer (perform reduce on the right</span></span><br><span class="line"><span class="comment">                 * half of the current window)</span></span><br><span class="line"><span class="comment">                 */</span></span><br><span class="line">                scount[step] = wsize / <span class="number">2</span>;</span><br><span class="line">                rcount[step] = wsize - scount[step];</span><br><span class="line">                rindex[step] = sindex[step] + scount[step];</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="comment">/* Send part of data from the rbuf, recv into the tmp_buf */</span></span><br><span class="line">            <span class="built_in">ompi_coll_base_sendrecv</span>((<span class="type">char</span> *)rbuf + (<span class="type">ptrdiff_t</span>)sindex[step] * extent,</span><br><span class="line">                            scount[step], dtype, dest, ...,</span><br><span class="line">                            (<span class="type">char</span> *)tmp_buf + (<span class="type">ptrdiff_t</span>)rindex[step] * extent, </span><br><span class="line">                            rcount[step], dtype, dest, ..., comm, ..., rank);</span><br><span class="line"></span><br><span class="line">            <span class="comment">/* Local reduce: rbuf[] = tmp_buf[] &lt;op&gt; rbuf[] */</span></span><br><span class="line">            <span class="built_in">ompi_op_reduce</span>(op, (<span class="type">char</span>*)tmp_buf + (<span class="type">ptrdiff_t</span>)rindex[step] * extent,</span><br><span class="line">                           (<span class="type">char</span> *)rbuf + (<span class="type">ptrdiff_t</span>)rindex[step] * extent,</span><br><span class="line">                           rcount[step], dtype);</span><br><span class="line"></span><br><span class="line">            <span class="comment">/* Move the current window to the received message */</span></span><br><span class="line">            <span class="keyword">if</span> (step + <span class="number">1</span> &lt; nsteps) &#123;</span><br><span class="line">                rindex[step + <span class="number">1</span>] = rindex[step];</span><br><span class="line">                sindex[step + <span class="number">1</span>] = rindex[step];</span><br><span class="line">                wsize = rcount[step];</span><br><span class="line">                step++;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">/*</span></span><br><span class="line"><span class="comment">        * Step 3. Allgather by the recursive doubling algorithm.</span></span><br><span class="line"><span class="comment">        * Each process has 1 / p&#x27; of the total reduction result:</span></span><br><span class="line"><span class="comment">        * rcount[nsteps - 1] elements in the rbuf[rindex[nsteps - 1], ...].</span></span><br><span class="line"><span class="comment">        * All exchanges are executed in reverse order relative to recursive doubling (previous step).</span></span><br><span class="line"><span class="comment">        */</span></span><br><span class="line">        step = nsteps - <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> mask = nprocs_pof2 &gt;&gt; <span class="number">1</span>; mask &gt; <span class="number">0</span>; mask &gt;&gt;= <span class="number">1</span>)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="type">int</span> vdest = vrank ^ mask;</span><br><span class="line">            <span class="comment">/* Translate vdest virtual rank to real rank */</span></span><br><span class="line">            <span class="type">int</span> dest = (vdest &lt; nprocs_rem) ? vdest * <span class="number">2</span> : vdest + nprocs_rem;</span><br><span class="line">            <span class="comment">/* Send rcount[step] elements from rbuf[rindex[step]...]</span></span><br><span class="line"><span class="comment">               Recv scount[step] elements to rbuf[sindex[step]...] */</span></span><br><span class="line">            <span class="built_in">ompi_coll_base_sendrecv</span>((<span class="type">char</span> *)rbuf + (<span class="type">ptrdiff_t</span>)rindex[step] * extent,</span><br><span class="line">                                rcount[step], dtype, dest, ...,</span><br><span class="line">                                (<span class="type">char</span> *)rbuf + (<span class="type">ptrdiff_t</span>)sindex[step] * extent,</span><br><span class="line">                                scount[step], dtype, dest, ..., comm, ..., rank);</span><br><span class="line">            step--;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/* Step 4. Send total result to excluded odd ranks. */</span></span><br><span class="line">    <span class="keyword">if</span> (rank &lt; <span class="number">2</span> * nprocs_rem)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">if</span>(rank % <span class="number">2</span> != <span class="number">0</span>) <span class="comment">/* Odd process -- recv result from rank - 1 */</span></span><br><span class="line">            <span class="built_in">MCA_PML_CALL</span>(<span class="built_in">recv</span>(rbuf, count, dtype, rank - <span class="number">1</span>, ..., comm, ...));</span><br><span class="line">        <span class="keyword">else</span> <span class="comment">/* Even process -- send result to rank + 1 */</span></span><br><span class="line">            <span class="built_in">MCA_PML_CALL</span>(<span class="built_in">send</span>(rbuf, count, dtype, rank + <span class="number">1</span>, ..., comm));</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//free buffer rindex, sindex, rcount, scount;</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="5、ring算法"><a href="#5、ring算法" class="headerlink" title="5、ring算法"></a>5、ring算法</h2><h3 id="OpenMPI版-3"><a href="#OpenMPI版-3" class="headerlink" title="OpenMPI版"></a>OpenMPI版</h3><p>注：只有在$OpenMPI$中才有$Ring$ $Allreduce$。</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"> * 消息自动被分成M/N段，算法执行2*N - 1步。算法不能保证规约操作顺序，所以仅支持满足交换律的操作，并且消息量小于进程数时算法不能正常工作。</span></span><br><span class="line"><span class="comment"> * Example on 5 nodes:</span></span><br><span class="line"><span class="comment"> *         Initial state</span></span><br><span class="line"><span class="comment"> *   #      0              1             2              3             4</span></span><br><span class="line"><span class="comment"> *        [00]           [10]          [20]           [30]           [40]</span></span><br><span class="line"><span class="comment"> *        [01]           [11]          [21]           [31]           [41]</span></span><br><span class="line"><span class="comment"> *        [02]           [12]          [22]           [32]           [42]</span></span><br><span class="line"><span class="comment"> *        [03]           [13]          [23]           [33]           [43]</span></span><br><span class="line"><span class="comment"> *        [04]           [14]          [24]           [34]           [44]</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> *  计算阶段</span></span><br><span class="line"><span class="comment"> *  Step 0: rank r sends block r to rank (r+1) and receives bloc (r-1)</span></span><br><span class="line"><span class="comment"> *  from rank (r-1) [with wraparound].</span></span><br><span class="line"><span class="comment"> *    #     0              1             2              3             4</span></span><br><span class="line"><span class="comment"> *        [00]          [00+10]        [20]           [30]           [40]</span></span><br><span class="line"><span class="comment"> *        [01]           [11]         [11+21]         [31]           [41]</span></span><br><span class="line"><span class="comment"> *        [02]           [12]          [22]          [22+32]         [42]</span></span><br><span class="line"><span class="comment"> *        [03]           [13]          [23]           [33]         [33+43]</span></span><br><span class="line"><span class="comment"> *      [44+04]          [14]          [24]           [34]           [44]</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> *  Step 1: rank r sends block (r-1) to rank (r+1) and receives bloc</span></span><br><span class="line"><span class="comment"> *  (r-2) from rank (r-1) [with wraparound].</span></span><br><span class="line"><span class="comment"> *    #      0              1             2              3             4</span></span><br><span class="line"><span class="comment"> *         [00]          [00+10]     [01+10+20]        [30]           [40]</span></span><br><span class="line"><span class="comment"> *         [01]           [11]         [11+21]      [11+21+31]        [41]</span></span><br><span class="line"><span class="comment"> *         [02]           [12]          [22]          [22+32]      [22+32+42]</span></span><br><span class="line"><span class="comment"> *      [33+43+03]        [13]          [23]           [33]         [33+43]</span></span><br><span class="line"><span class="comment"> *        [44+04]       [44+04+14]       [24]           [34]           [44]</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> *  Step 2: rank r sends block (r-2) to rank (r+1) and receives bloc</span></span><br><span class="line"><span class="comment"> *  (r-2) from rank (r-1) [with wraparound].</span></span><br><span class="line"><span class="comment"> *    #      0              1             2              3             4</span></span><br><span class="line"><span class="comment"> *         [00]          [00+10]     [01+10+20]    [01+10+20+30]      [40]</span></span><br><span class="line"><span class="comment"> *         [01]           [11]         [11+21]      [11+21+31]    [11+21+31+41]</span></span><br><span class="line"><span class="comment"> *     [22+32+42+02]      [12]          [22]          [22+32]      [22+32+42]</span></span><br><span class="line"><span class="comment"> *      [33+43+03]    [33+43+03+13]     [23]           [33]         [33+43]</span></span><br><span class="line"><span class="comment"> *        [44+04]       [44+04+14]  [44+04+14+24]      [34]           [44]</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> *  Step 3: rank r sends block (r-3) to rank (r+1) and receives bloc</span></span><br><span class="line"><span class="comment"> *   (r-3) from rank (r-1) [with wraparound].</span></span><br><span class="line"><span class="comment"> *    #      0              1             2              3             4</span></span><br><span class="line"><span class="comment"> *         [00]          [00+10]     [01+10+20]    [01+10+20+30]     [FULL]</span></span><br><span class="line"><span class="comment"> *        [FULL]           [11]        [11+21]      [11+21+31]    [11+21+31+41]</span></span><br><span class="line"><span class="comment"> *     [22+32+42+02]     [FULL]          [22]         [22+32]      [22+32+42]</span></span><br><span class="line"><span class="comment"> *      [33+43+03]    [33+43+03+13]     [FULL]          [33]         [33+43]</span></span><br><span class="line"><span class="comment"> *        [44+04]       [44+04+14]  [44+04+14+24]      [FULL]         [44]</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> *  分布阶段：ring ALLGATHER with ranks shifted by 1.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">ompi_coll_base_allreduce_intra_ring</span><span class="params">(...)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="type">int</span> size = <span class="built_in">ompi_comm_size</span>(comm);</span><br><span class="line">    <span class="type">int</span> rank = <span class="built_in">ompi_comm_rank</span>(comm);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (<span class="number">1</span> == size) <span class="comment">//copy from sbuf to rbuf</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//若消息量小于进程数就使用recursive doubling</span></span><br><span class="line">    <span class="keyword">if</span> (count &lt; size) </span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">ompi_coll_base_allreduce_intra_recursivedoubling</span>(sbuf, rbuf, count, dtype, op, comm, <span class="keyword">module</span>);</span><br><span class="line"></span><br><span class="line">    </span><br><span class="line">    </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
    </div>

    
    
    
        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>虎王
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://chudod.gitee.io/works/2022/04/13/OpenMPI&MPICH_Allreduce/" title="MPI_Allreduce的在OpenMPI、MPICH中的实现">https://chudod.gitee.io/works/2022/04/13/OpenMPI&MPICH_Allreduce/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>

        

  <div class="followme">
    <p>欢迎关注我的其它发布渠道</p>

    <div class="social-list">

        <div class="social-item">
          <a target="_blank" class="social-link" href="/images/wechat_channel.jpg">
            <span class="icon">
              <i class="fab fa-weixin"></i>
            </span>

            <span class="label">WeChat</span>
          </a>
        </div>

        <div class="social-item">
          <a target="_blank" class="social-link" href="/atom.xml">
            <span class="icon">
              <i class="fa fa-rss"></i>
            </span>

            <span class="label">RSS</span>
          </a>
        </div>
    </div>
  </div>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/works/tags/MPI/" rel="tag"># MPI</a>
              <a href="/works/tags/%E5%B9%B6%E8%A1%8C%E8%AE%A1%E7%AE%97/" rel="tag"># 并行计算</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/works/2022/04/10/Guiding_Board/" rel="prev" title="Read Me（导航栏）">
      <i class="fa fa-chevron-left"></i> Read Me（导航栏）
    </a></div>
      <div class="post-nav-item">
    <a href="/works/2022/04/14/OS_Memory_Management/" rel="next" title="操作系统内存管理">
      操作系统内存管理 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#Allreduce%E7%AE%97%E6%B3%95%E5%88%97%E8%A1%A8"><span class="nav-number">1.</span> <span class="nav-text">Allreduce算法列表</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1%E3%80%81OpenMPI-4-1%E4%B8%AD%E7%9A%84MPI-Allreduce%E7%AE%97%E6%B3%95"><span class="nav-number">1.1.</span> <span class="nav-text">1、OpenMPI-4.1中的MPI_Allreduce算法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2%E3%80%81MPICH%E4%B8%AD%E7%9A%84MPI-Allreduce%E7%AE%97%E6%B3%95"><span class="nav-number">1.2.</span> <span class="nav-text">2、MPICH中的MPI_Allreduce算法</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#Allreduce%E7%AE%97%E6%B3%95%E5%AE%9E%E7%8E%B0"><span class="nav-number">2.</span> <span class="nav-text">Allreduce算法实现</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1%E3%80%81-Reduce-Broadcast-%E7%AE%97%E6%B3%95"><span class="nav-number">2.1.</span> <span class="nav-text">1、$Reduce$+$Broadcast$算法</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#OpenMPI%E7%89%88"><span class="nav-number">2.1.1.</span> <span class="nav-text">OpenMPI版</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#basic-linear%E5%87%BD%E6%95%B0"><span class="nav-number">2.1.1.1.</span> <span class="nav-text">basic_linear函数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#nonoverlapping%E5%87%BD%E6%95%B0"><span class="nav-number">2.1.1.2.</span> <span class="nav-text">nonoverlapping函数</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#MPICH%E7%89%88"><span class="nav-number">2.1.2.</span> <span class="nav-text">MPICH版</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link"><span class="nav-number">2.1.2.1.</span> <span class="nav-text"></span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2%E3%80%81-Recursive-Doubling-%E7%AE%97%E6%B3%95"><span class="nav-number">2.2.</span> <span class="nav-text">2、$Recursive$ $Doubling$算法</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#OpenMPI%E7%89%88-1"><span class="nav-number">2.2.1.</span> <span class="nav-text">OpenMPI版</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#recursivedoubling%E5%87%BD%E6%95%B0"><span class="nav-number">2.2.1.1.</span> <span class="nav-text">recursivedoubling函数</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#MPICH%E7%89%88-1"><span class="nav-number">2.2.2.</span> <span class="nav-text">MPICH版</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4%E3%80%81reduce-scatter%E7%AE%97%E6%B3%95"><span class="nav-number">2.3.</span> <span class="nav-text">4、reduce-scatter算法</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#OpenMPI%E7%89%88-2"><span class="nav-number">2.3.1.</span> <span class="nav-text">OpenMPI版</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5%E3%80%81ring%E7%AE%97%E6%B3%95"><span class="nav-number">2.4.</span> <span class="nav-text">5、ring算法</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#OpenMPI%E7%89%88-3"><span class="nav-number">2.4.1.</span> <span class="nav-text">OpenMPI版</span></a></li></ol></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="虎王"
      src="/works/images/avatar.png">
  <p class="site-author-name" itemprop="name">虎王</p>
  <div class="site-description" itemprop="description">喜欢造飞机导弹和计算机的大学生</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/works/archives/">
        
          <span class="site-state-item-count">21</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/works/categories/">
          
        <span class="site-state-item-count">13</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/works/tags/">
          
        <span class="site-state-item-count">12</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/DoD-Chu/" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;DoD-Chu&#x2F;" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:1368095113@qq.com" title="E-Mail → mailto:1368095113@qq.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://www.zhihu.com/people/pu-ti-shan-cheng-zhu" title="知乎 → https:&#x2F;&#x2F;www.zhihu.com&#x2F;people&#x2F;pu-ti-shan-cheng-zhu" rel="noopener" target="_blank"><i class="fab fa-stack-overflow fa-fw"></i>知乎</a>
      </span>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title"><i class="fa fa-link fa-fw"></i>
      其他站点
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="https://www.zhihu.com/people/pu-ti-shan-cheng-zhu" title="https:&#x2F;&#x2F;www.zhihu.com&#x2F;people&#x2F;pu-ti-shan-cheng-zhu" rel="noopener" target="_blank">知乎主页</a>
        </li>
    </ul>
  </div>

      </div>
        <div class="back-to-top motion-element">
          <i class="fa fa-arrow-up"></i>
          <span>0%</span>
        </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; Fri Mar 25 2022 08:00:00 GMT+0800 (中国标准时间) – 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">虎王</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">博客全站共30.4k字</span>
</div>


    <script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

    <span id="busuanzi_container_site_pv">总访问量<span id="busuanzi_value_site_pv"></span>次</span>
    <span class="post-meta-divider">|</span>
    <span id="busuanzi_container_site_uv">总访客数<span id="busuanzi_value_site_uv"></span>人</span>
    <span class="post-meta-divider">|</span>
<!-- 不蒜子计数初始值纠正 -->
<script>
$(document).ready(function() {

    var int = setInterval(fixCount, 50);  // 50ms周期检测函数
    var countOffset = 20000;  // 初始化首次数据

    function fixCount() {            
       if (document.getElementById("busuanzi_container_site_pv").style.display != "none")
        {
            $("#busuanzi_value_site_pv").html(parseInt($("#busuanzi_value_site_pv").html()) + countOffset); 
            clearInterval(int);
        }                  
        if ($("#busuanzi_container_site_pv").css("display") != "none")
        {
            $("#busuanzi_value_site_uv").html(parseInt($("#busuanzi_value_site_uv").html()) + countOffset); // 加上初始数据 
            clearInterval(int); // 停止检测
        }  
    }
       	
});
</script> 

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  
  <script color='0,0,255' opacity='0.5' zIndex='-1' count='99' src="/works/lib/canvas-nest/canvas-nest.min.js"></script>
  <script src="/works/lib/anime.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js"></script>
  <script src="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/lozad@1/dist/lozad.min.js"></script>
  <script src="/works/lib/velocity/velocity.min.js"></script>
  <script src="/works/lib/velocity/velocity.ui.min.js"></script>

<script src="/works/js/utils.js"></script>

<script src="/works/js/motion.js"></script>


<script src="/works/js/schemes/pisces.js"></script>


<script src="/works/js/next-boot.js"></script>

<script src="/works/js/bookmark.js"></script>




  
  <script>
    (function(){
      var canonicalURL, curProtocol;
      //Get the <link> tag
      var x=document.getElementsByTagName("link");
		//Find the last canonical URL
		if(x.length > 0){
			for (i=0;i<x.length;i++){
				if(x[i].rel.toLowerCase() == 'canonical' && x[i].href){
					canonicalURL=x[i].href;
				}
			}
		}
    //Get protocol
	    if (!canonicalURL){
	    	curProtocol = window.location.protocol.split(':')[0];
	    }
	    else{
	    	curProtocol = canonicalURL.split(':')[0];
	    }
      //Get current URL if the canonical URL does not exist
	    if (!canonicalURL) canonicalURL = window.location.href;
	    //Assign script content. Replace current URL with the canonical URL
      !function(){var e=/([http|https]:\/\/[a-zA-Z0-9\_\.]+\.baidu\.com)/gi,r=canonicalURL,t=document.referrer;if(!e.test(r)){var n=(String(curProtocol).toLowerCase() === 'https')?"https://sp0.baidu.com/9_Q4simg2RQJ8t7jm9iCKT-xh_/s.gif":"//api.share.baidu.com/s.gif";t?(n+="?r="+encodeURIComponent(document.referrer),r&&(n+="&l="+r)):r&&(n+="?l="+r);var i=new Image;i.src=n}}(window);})();
  </script>




  
<script src="//cdn.jsdelivr.net/npm/algoliasearch@4/dist/algoliasearch-lite.umd.js"></script>
<script src="//cdn.jsdelivr.net/npm/instantsearch.js@4/dist/instantsearch.production.min.js"></script>
<script src="/works/js/algolia-search.js"></script>










<script>
document.querySelectorAll('.pdfobject-container').forEach(element => {
  let url = element.dataset.target;
  let pdfOpenParams = {
    navpanes : 0,
    toolbar  : 0,
    statusbar: 0,
    pagemode : 'thumbs',
    view     : 'FitH'
  };
  let pdfOpenFragment = '#' + Object.entries(pdfOpenParams).map(([key, value]) => `${key}=${encodeURIComponent(value)}`).join('&');
  let fullURL = `/works/lib/pdf/web/viewer.html?file=${encodeURIComponent(url)}${pdfOpenFragment}`;

  if (NexT.utils.supportsPDFs()) {
    element.innerHTML = `<embed class="pdfobject" src="${url + pdfOpenFragment}" type="application/pdf" style="height: ${element.dataset.height};">`;
  } else {
    element.innerHTML = `<iframe src="${fullURL}" style="height: ${element.dataset.height};" frameborder="0"></iframe>`;
  }
});
</script>


<script>
if (document.querySelectorAll('pre.mermaid').length) {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/mermaid@8/dist/mermaid.min.js', () => {
    mermaid.initialize({
      theme    : 'forest',
      logLevel : 3,
      flowchart: { curve     : 'linear' },
      gantt    : { axisFormat: '%m/%d/%Y' },
      sequence : { actorMargin: 50 }
    });
  }, window.mermaid);
}
</script>


  

  
      

<script>
  if (typeof MathJax === 'undefined') {
    window.MathJax = {
      loader: {
        source: {
          '[tex]/amsCd': '[tex]/amscd',
          '[tex]/AMScd': '[tex]/amscd'
        }
      },
      tex: {
        inlineMath: {'[+]': [['$', '$']]},
        tags: 'ams'
      },
      options: {
        renderActions: {
          findScript: [10, doc => {
            document.querySelectorAll('script[type^="math/tex"]').forEach(node => {
              const display = !!node.type.match(/; *mode=display/);
              const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display);
              const text = document.createTextNode('');
              node.parentNode.replaceChild(text, node);
              math.start = {node: text, delim: '', n: 0};
              math.end = {node: text, delim: '', n: 0};
              doc.math.push(math);
            });
          }, '', false],
          insertedScript: [200, () => {
            document.querySelectorAll('mjx-container').forEach(node => {
              let target = node.parentNode;
              if (target.nodeName.toLowerCase() === 'li') {
                target.parentNode.classList.add('has-jax');
              }
            });
          }, '', false]
        }
      }
    };
    (function () {
      var script = document.createElement('script');
      script.src = '//cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js';
      script.defer = true;
      document.head.appendChild(script);
    })();
  } else {
    MathJax.startup.document.state(0);
    MathJax.texReset();
    MathJax.typeset();
  }
</script>

    

  

</body>
</html>
